package com.liyuhan.onlinetaxibackground.Service.impl;

import com.liyuhan.onlinetaxibackground.POJO.User;
import com.liyuhan.onlinetaxibackground.Repository.UserRepository;
import com.liyuhan.onlinetaxibackground.Service.LoginService;
import com.liyuhan.onlinetaxibackground.Util.GetRequestBody;
import com.liyuhan.onlinetaxibackground.dto.DataRequest;
import com.liyuhan.onlinetaxibackground.dto.DataResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginImpl implements LoginService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private OrderImpl order;

    // 响应体
    private DataResponse dataResponse;
    // 响应数据
    private Map<String, String> map = new HashMap<>();

    @Override
    public DataResponse loginWithPsw(String request) {
        // 获取请求体
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        // 清空map
        map.clear();

        // 手机号
        String phone = requestBody.getString("phone");
        // 密码
        String password = requestBody.getString("password");

        // 对用户输入的密码进行校验
        if (userRepository.findUserByPhone(phone).isPresent()) {
            // 判断手机号是否已经注册
            User user = userRepository.findUserByPhone(phone).get();
            if (user.getPassword().equals(password)) {
                // 用户密码正确
                // 返回用户类型
                map.put("type", user.getType());

                // 设置用户登录状态
                user.setState(true);

                // 保存对用户的修改
                userRepository.save(user);

                dataResponse = new DataResponse("200", map);
            } else {
                // 用户密码不正确
                map.put("error", "密码错误！");

                dataResponse = new DataResponse("400", map);
                return dataResponse;
            }
        } else {
            // 手机号未注册
            map.put("error", "手机号未注册！");

            dataResponse = new DataResponse("400", map);
            return dataResponse;
        }

        return dataResponse;
    }

    @Override
    public DataResponse loginWithVerifyCode(String request) {
        // 解析请求体
        DataRequest dataRequest = GetRequestBody.getRequestBody(request);

        // 清空map
        map.clear();

        // 取出请求参数
        // 验证码
        String verifyCode = dataRequest.get("verifyCode").toString();
        // 手机号
        String phone = dataRequest.get("phone").toString();

        // 验证码检验
        if (!RegisterImpl.getRedisCode(phone, verifyCode)) {
            // 用户未通过验证
            Map<String, String> map = new HashMap<>();
            map.put("error", "用户验证码错误！！");
            dataResponse = new DataResponse("400", map);
            // 直接返回
            return dataResponse;
        }

        // 获取用户
        User user = userRepository.findUserByPhone(phone).get();
        // 设置用户登录状态
        user.setState(true);

        try {
            // 添加到数据库
            userRepository.save(user);
        } catch (Exception e) {
            Map<String, String> map = new HashMap<>();
            map.put("error", "插入数据库错误！！");

            dataResponse = new DataResponse("400", map);

            return dataResponse;
        }

        // 用户登录成功
        Map<String, String> map = new HashMap<>();
        map.put("success", "用户登录成功！！");
        map.put("type", user.getType());
        dataResponse = new DataResponse("200", map);

        return dataResponse;
    }

    @Override
    public DataResponse isLogin(String request) {
        // 请求体
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        // 清空map
        map.clear();

        // 获得手机号
        String phone = requestBody.getString("phone");

        // 验证手机号的登录状态
        if (userRepository.findUserByPhone(phone).isPresent()) {
            // 用户存在
            User user = userRepository.findUserByPhone(phone).get();

            // 用户状态为登录态
            if (user.getState()) {
                map.put("type", user.getType());

                // 返回成功信息
                dataResponse = new DataResponse("200", map);
            } else {
                dataResponse = new DataResponse("500", null);
            }
        } else {
            // 用户不存在
            dataResponse = new DataResponse("500", null);
        }

        return dataResponse;
    }

    @Override
    public void exit(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        // 清空map
        map.clear();

        String phone = requestBody.getData().get("phone").toString();

        // 修改用户登录状态
        User user = userRepository.findUserByPhone(phone).get();
        user.setState(false);
        userRepository.save(user);
    }
}
